package com.ruoyi.web.controller.safety;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.xuehua.SnowflakeIdUtils;
import com.ruoyi.safety.domain.Area;
import com.ruoyi.safety.domain.Mine;
import com.ruoyi.safety.service.IAreaService;
import com.ruoyi.safety.service.IMineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 煤矿管理Controller
 * 
 * @author ruoyi
 * @date 2024-03-11
 */
@RestController
@RequestMapping("/mine/mine")
public class MineController extends BaseController
{
    @Autowired
    private IMineService mineService;
    @Autowired
    private IAreaService areaService;
    /**
     * 查询煤矿管理列表
     */
    @PreAuthorize("@ss.hasPermi('mine:mine:list')")
    @GetMapping("/list")
    public TableDataInfo list(Mine mine)
    {
        startPage();
        List<Mine> list = mineService.selectMineList(mine);
        return getDataTable(list);
    }

    /**
     * 导出煤矿管理列表
     */
    @PreAuthorize("@ss.hasPermi('mine:mine:export')")
    @Log(title = "煤矿管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Mine mine)
    {
        List<Mine> list = mineService.selectMineList(mine);
        ExcelUtil<Mine> util = new ExcelUtil<Mine>(Mine.class);
        util.exportExcel(response, list, "煤矿管理数据");
    }

    /**
     * 获取煤矿管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('mine:mine:query')")
    @GetMapping(value = "/{mineId}")
    public AjaxResult getInfo(@PathVariable("mineId") String mineId)
    {
        return success(mineService.selectMineByMineId(mineId));
    }

    /**
     * 新增煤矿管理
     */
    @PreAuthorize("@ss.hasPermi('mine:mine:add')")
    @Log(title = "煤矿管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Mine mine)
    {
        // 校验数据库是否存在相同数据
        Mine mine1 = mineService.getMineByName(mine.getMineName());
        if (mine1 != null){
            return error("已有相同名称的煤矿，不能再次添加");
        }
        mine.setMineId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        return toAjax(mineService.insertMine(mine));
    }

    /**
     * 修改煤矿管理
     */
    @PreAuthorize("@ss.hasPermi('mine:mine:edit')")
    @Log(title = "煤矿管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Mine mine)
    {
        return toAjax(mineService.updateMine(mine));
    }

    /**
     * 删除煤矿管理
     */
    @PreAuthorize("@ss.hasPermi('mine:mine:remove')")
    @Log(title = "煤矿管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{mineIds}")
    public AjaxResult remove(@PathVariable String[] mineIds)
    {
        for (String mineId : mineIds) {
            // 获取区域信息通过煤矿ID
            List<Area> areaList =  areaService.getAreaByMineId(mineId);
            if (areaList.size() > 0){
                return error("该煤矿已经开始运作，不能删除");
            }
        }
        return toAjax(mineService.deleteMineByMineIds(mineIds));
    }
}
